import 'dart:convert';

import 'package:dali_ng_pera/manager/dnp_plugin_manager.dart';
import 'package:dali_ng_pera/net/dnp_net_manager.dart';
import 'package:dali_ng_pera/page/certification/model/dnp_auton_model.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:dio/dio.dart' as dio;

class DnpCertificationController extends GetxController {

  final autonModel = DnpAutonModel().obs;
  final agreementCheck = true.obs;

  DnpEnwrapped? get enwrapped {
    return autonModel.value.enwrapped;
  }

  DnpDetailItem? get hastily {
    return enwrapped?.string?.hastily;
  }

  DnpDetailItem? get tea {
    return enwrapped?.string?.tea;
  }

  String get curDeveloping {
    return autonModel.value.boldly?.developing ?? "";
  }

  String get parcel {
    return enwrapped?.parcel ?? "";
  }

  List<DnpCrossItem> get crossList {
    List<DnpCrossItem> list = autonModel.value.cross ?? [];
    if (list.isEmpty || list.length < 5) {
      return [
        DnpCrossItem(
          discernible: "Identity information",
          developing: "wasa",
        ),
        DnpCrossItem(
          discernible: "Personal information",
          developing: "wasb",
        ),
        DnpCrossItem(
          discernible: "Job information",
          developing: "wasc",
        ),
        DnpCrossItem(
          discernible: "Emergency contact",
          developing: "wasd",
        ),
        DnpCrossItem(
          discernible: "Bank card",
          developing: "wase",
        ),
      ];
    }
    return list;
  }

  void onRequestData(String already) {
    DnpPluginManager().showLoading();
    DnpNetManager()
        .post("/haseye/auton", data: dio.FormData.fromMap({"already": already}))
        .then((value) {
      DnpPluginManager().hideLoading();
      if (value.success) {
        try {
          autonModel.value = DnpAutonModel.fromJson(value.close);
        } catch (e) {
          DnpPluginManager().showToast(e.toString());
        }
      } else {
        DnpPluginManager().showToast(value.kurios ?? "");
      }
    });
  }

  void onRequestPlaceAnOrder(String occurred, String soundly, String heart,
      String yeared, ValueChanged<String> callback) {
    DnpPluginManager().showLoading();
    DnpNetManager()
        .post("/haseye/theos",
            data: dio.FormData.fromMap({
              "occurred": occurred,
              "soundly": soundly,
              "heart": heart,
              "yeared": yeared,
            }))
        .then((value) {
      DnpPluginManager().hideLoading();
      if (value.success) {
        try {
          callback.call(value.close["belated"] ?? "");
        } catch (e) {
          DnpPluginManager().showToast(e.toString());
        }
      } else {
        DnpPluginManager().showToast(value.kurios ?? "");
      }
    });
  }

  String getDeveloping({int? index}){
    String curDeveloping = autonModel.value.boldly?.developing ?? "";
    if(index == null){
      return curDeveloping;
    }
    if(curDeveloping.isEmpty){
      return crossList[index].developing ?? "";
    }
    return curDeveloping;
  }

  String getBelated({int? index}){
    String curDeveloping = autonModel.value.boldly?.developing ?? "";

    if(index == null){
      return autonModel.value.boldly?.belated ?? "";
    }
    if(curDeveloping.isEmpty){
      return crossList[index].belated ?? "";
    }
    return autonModel.value.boldly?.belated ?? "";;
  }
}
